//
//  IGAddPhotoCell.m
//  jingdongfang
//
//  Created by MTiMac02 on 2017/11/3.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGAddPhotoCell.h"
#import "CollecteWorkModel.h"
#import "UploadWorkModel.h"

@interface IGAddPhotoCell ()
{
    UIImageView *imageView;
    UILabel *name;
    UILabel *author;//作者
    UIButton *addBtn;

}

@end
@implementation IGAddPhotoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self _initViews];
    }
    return self;
}

- (void)_initViews
{
    self.contentView.backgroundColor = [UIColor c3Color];
    imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    [self.contentView addSubview:imageView];
    
    name = [[UILabel alloc] initWithFrame:CGRectZero];
    name.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:name];
    
    author = [[UILabel alloc] initWithFrame:CGRectZero];
    author.font = [UIFont systemFontOfSize:13];
    author.textColor = [UIColor grayColor];
    [self.contentView addSubview:author];
    
     addBtn=[UIButton buttonWithType:UIButtonTypeCustom];
     addBtn.clipsToBounds=YES;
     addBtn.layer.borderWidth=1.0f;
     addBtn.layer.cornerRadius=15.f;
    addBtn.titleLabel.font=[UIFont font14];
    [addBtn setTitleColor:[UIColor c5Color] forState:UIControlStateSelected];
     [addBtn setTitleColor:[UIColor c1Color] forState:UIControlStateNormal];
//     [addBtn setTitle:@"已添加" forState:UIControlStateSelected];
    [addBtn setTitle:NSLocalizedString(@"Added", nil) forState:UIControlStateSelected];
    [addBtn setTitle:@"+" forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [addBtn setSelected:NO];
    [self addSubview:addBtn];
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if ([self.data isMemberOfClass:[CollecteWorkModel class]])
    {
        CollecteWorkModel *info = self.collectInfo;

        [imageView sd_setImageWithURL:[NSURL URLWithString:info.image]];
        imageView.backgroundColor=[UIColor c5Color];
        imageView.frame = CGRectMake(0, 0, self.height, self.height);
        
        name.text = info.title?:@" ";
        [name sizeToFit];
        name.x = imageView.right + 10;
        name.y = imageView.top + 10;
        name.width = 150;
    
//        author.text = [NSString stringWithFormat:@"作者名·%@",info.userNm];
        author.text = [NSString stringWithFormat:@"%@·%@",NSLocalizedString(@"Author",nil),info.userNm];
        [author sizeToFit];
        author.width=150;
        author.x = imageView.right + 10;
        author.top = name.bottom + 5;
    
        [addBtn sizeToFit];
        addBtn.width=60;
        addBtn.centerY=self.height/2;
        addBtn.x=KSCREENWIDTH-15-addBtn.width;
        addBtn.layer.borderColor=[UIColor c1Color].CGColor;
       if ([info.ifExist integerValue]==1) {
           [addBtn setSelected:YES];
           addBtn.layer.borderColor=[UIColor c5Color].CGColor;
         }
       if ([info.ifExist integerValue]==0)
        {
           addBtn.layer.borderColor=[UIColor c1Color].CGColor;
           [addBtn setSelected:NO];
           
         }
    }
    if ([self.data isMemberOfClass:[UploadWorkModel class]]) {
        UploadWorkModel *upInfo = self.upInfo;
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:upInfo.image]];
        imageView.backgroundColor=[UIColor c5Color];
        imageView.frame = CGRectMake(0, 0, self.height, self.height);
        
        name.text = upInfo.title?:@" ";
        [name sizeToFit];
        name.x = imageView.right + 10;
        name.y = imageView.top + 10;
        name.width = 150;
        
//        author.text = [NSString stringWithFormat:@"作者名·%@",upInfo.userNm];
        author.text = [NSString stringWithFormat:@"%@·%@",NSLocalizedString(@"Author",nil),upInfo.userNm];
        [author sizeToFit];
        author.width=150;
        author.x = imageView.right + 10;
        author.top = name.bottom + 5;
        
        [addBtn sizeToFit];
        addBtn.width=60;
        addBtn.centerY=self.height/2;
        addBtn.x=KSCREENWIDTH-15-addBtn.width;
        addBtn.layer.borderColor=[UIColor c1Color].CGColor;
        if ([upInfo.ifExist integerValue]==1) {
            [addBtn setSelected:YES];
//            addBtn.userInteractionEnabled=NO;
            addBtn.layer.borderColor=[UIColor c5Color].CGColor;
        }
        if ([upInfo.ifExist integerValue]==0)
        {
            [addBtn setSelected:NO];
            addBtn.layer.borderColor=[UIColor c1Color].CGColor;
        }

    }

}

-(void)addBtnClick:(UIButton *)btn{
    __weak typeof(self) weakSelf = self;
//    [addBtn setTapActionWithBlock:^{
        if (weakSelf.attenBlock) {
            weakSelf.attenBlock();
        }
}
//
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
